import axios, {AxiosResponse} from "axios";
import {BackendPageModel} from "@/models/BackendPageModel";
import BomModel from "@/models/BomModel";
import StoreOutModel from "@/models/StoreOutModel";
import TencentOcrResultModel from "@/models/TencentOcrResultModel";
import {StoreOutRequestModel} from "@/models/StoreOutRequestModel";

export function selectOut(search = '', pageNum = 1, pageSize = 10,): Promise<BackendPageModel<StoreOutModel>> {
    return new Promise((resolve, reject) => {
        axios({
            method: 'get',
            url: '/api/out/page',
            params: {pageNumber: pageNum - 1, pageSize: pageSize, codeOrName: search}
        }).then(value => {
            resolve(value.data);
        }).catch(reason => {
            console.log(reason);
            reject();
        })
    })
}

export async function selectOutCode() {
    const result = await axios({method: 'get', url: '/api/out/code'})
    console.log(result)
    return result;
}

export function ocrStoreOut(file: File): Promise<TencentOcrResultModel> {
    return new Promise((resolve, reject) => {
        const form: FormData = new FormData();
        form.set('file', file);
        axios({method: 'post', url: '/api/out/ocr', data: form}).then((value: AxiosResponse<TencentOcrResultModel>) => {
            resolve(value.data);
        }).catch(reason => {
            console.log(reason);
            reject();
        })
    })
}

export function saveStoreOut(data: StoreOutRequestModel): Promise<StoreOutModel> {
    return new Promise((resolve, reject) => {
        axios({method: 'post', url: '/api/out', data}).then((value: AxiosResponse<StoreOutModel>) => {
            resolve(value.data);
        }).catch(reason => {
            console.log(reason);
            reject();
        })
    })
}

export function getOutDetailByOutId(id: number): Promise<BomModel> {
    return new Promise((resolve, reject) => {
        axios({method: 'get', url: `/api/out/${id}`}).then(value => {
            console.log(value)
            resolve(value.data);
        }).catch(reason => {
            console.log(reason);
            reject();
        })
    })
}

export function getOutDetailByOutCode(id: number): Promise<BomModel> {
    return new Promise((resolve, reject) => {
        axios({method: 'get', url: `/api/bom/${id}`}).then(value => {
            console.log(value)
            resolve(value.data);
        }).catch(reason => {
            console.log(reason);
            reject();
        })
    })
}